/* ************************************************************************
> File Name:     9.c
> Author:        zhfu
> mail:          3346897597@qq.com
> Created Time:  2022年04月30日 星期六 14时10分54秒
> Description:   
 ************************************************************************/
#include<stdio.h>
#include<stdlib.h>

char *my_stack[200];
int head = 0;

void push(char *s)
{
    my_stack[head++] = s;
}

char *pop()
{
    return my_stack[--head];
}

int is_empty()
{
    return head;
}

int main(int argc, char *argv[])
{
    FILE *f1, *f2;
    char *src = argv[1];
    char *dest = argv[2];

    if (!(f1 = fopen(src, "r"))){
        fputs("open file ", stderr);
        perror(src);
        exit(1);
    }

    if (!(f2 = fopen(dest, "w"))){
        fputs("open file ", stderr);
        perror(dest);
        exit(1);
    }

    char s1[200];
    while (fgets(s1, 200, f1)){
        char s2[210];
        if (s1[0] == ';'){
            int i = 1;
            while (s1[i] != '\n'){
                s1[i-1] = s1[i]; 
                ++i;
            }
            s1[i-1] = '\0';    

            sprintf(s2, "<!-- %s -->\n", s1);
            fputs(s2, f2);
        }
        else if (s1[0] == '['){
            int i = 1;
            while (s1[i] != ']'){
                s1[i-1] = s1[i];
                ++i;
            }
            s1[i-1] = '\0';
            sprintf(s2, "<%s>\n", s1);
            fputs(s2, f2);
            
            char s4[200];
            while (fgets(s4, 200, f1)){
                int k = 0;
                while (s4[k] != '\n'){
                    ++k;
                }
                if (k == 0 || s4[k-1] == ' ' || s4[k-1] == '\t'){
                    break;
                }

                char s5[200];
                char s6[200];
                int i = 0;
                while (s4[i] != '=' && s4[i] != ' ' && s4[i] != '\t'){
                    s5[i] = s4[i];
                    ++i;
                }
                s5[i] = '\0';
                
                int j = 0;
                while (s4[i] == ' ' || s4[i] == '\t' || s4[i] == '='){
                    ++i;
                }

                while (s4[i+1] != '\n'){
                    s6[j++] = s4[i+1];
                    ++i;
                }
                s6[j] = '\0';

                char s7[800];
                sprintf(s7, "\t\t<%s>%s</%s>\n", s5, s6, s5);
                fputs(s7, f2);
            }

            char s3[210];
            sprintf(s3, "</%s>\n", s1);
            fputs(s3, f2);
        }
    }


    fclose(f1);
    fclose(f2);

    return 0;
}
